package com.dingup.provider.service.sys.impl;

import com.dingup.api.adapter.user.SysUserAdapter;
import com.dingup.api.model.sys.SysSubject;
import com.dingup.api.model.sys.SysUserSubject;
import com.dingup.provider.mapper.SysSubjectMapper;
import com.dingup.provider.mapper.SysUserMapper;
import com.dingup.provider.mapper.SysUserSubjectMapper;
import com.dingup.provider.service.sys.SysSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by wancheng on 2018/11/28.
 */
@Service
public class SysSubjectServiceImpl implements SysSubjectService {

    @Autowired
    SysSubjectMapper sysSubjectMapper;
    @Autowired
    SysUserSubjectMapper sysUserSubjectMapper;
    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public List<SysSubject> getAllSysSubject() {
        return sysSubjectMapper.getAllSysSubject();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(String userId, List<Integer> sysSubjectIds) {
        sysUserSubjectMapper.deleteByUserIdAndSysSubjectId(userId);
        sysSubjectIds.forEach(
                id -> {
                    SysUserSubject sysUserSubject = new SysUserSubject();
                    sysUserSubject.setSysUserId(userId);
                    sysUserSubject.setSysSubjectId(id);
                    sysUserSubjectMapper.insertSelective(sysUserSubject);
                }
        );

    }

    @Override
    public List<SysUserAdapter> getBySysSubjectId(Integer subjectId) {
        List<SysUserAdapter> teachers = sysUserMapper.getBySysSubjectId(subjectId);
        return teachers;
    }
}
